<?php

namespace App\Model;

use App\Model\BaseModel;
use App\Model\Number;
use Illuminate\Support\Facades\DB;

class FlockModel extends BaseModel
{
    protected $connection = 'api';
    protected $table = 'group_chat';
    protected $primaryKey = 'family_member_id';

    public function index($input)
    {
        $page = $input['page'] ?? 1;
        $limit = $input['limit'] ?? 10;
        $uuid = $input['uuid'] ?? '';
        $flock_uid = $input['flock_uid']  ?? '';
        $where = [];
        if ($flock_uid) {
            $where[] = ['group_chat_uid', 'like', "%$flock_uid%"];
        }
        if ($uuid) {
            $where[] = ['uuid', 'like', "%$uuid%"];
        }
        $res = DB::connection($this->connection)->table($this->table)->where($where)
            ->leftJoin("user", "group_chat.user_id", "=", "user.user_id")
            ->leftJoin("group_chat_member", "group_chat_member.group_chat_id", "=", "group_chat.group_chat_id")
            ->select('group_chat.*', 'user.uuid', 'user.nickname')
            ->selectRaw('zb_group_chat_member.group_chat_id as nun_chat_id,count(zb_group_chat_member.group_chat_member_id) as count_users')
            ->groupBy('group_chat.group_chat_id')
            ->get();
        $data = $this->objToArray($res);
        $total = count($data);
        $data = collect($data)->forPage($page, $limit)->values()->toArray();
        foreach ($data as &$v) {
            $v['created_at'] = date('Y-m-d H:i:s', $v['created_at']);
        }
        return ['total' => $total, 'data' => $data, 'current_page' => intval($page)];
    }


    public function getRoomList($g_id)
    {
        $res = DB::connection($this->connection)->table('group_chat_room')->where(['group_chat_id' => $g_id])
            ->leftJoin("room", "room.room_id", "=", "group_chat_room.room_id")
            ->select('group_chat_room.*','room_uid','notice','image','desc','name')
            ->get();
        return $this->objToArray($res);
    }

    public function createData($data)
    {
        return curl_request(getKkaudioApiUrl() . '/inner/group_chat/upsert', $data, 'json', 'POST');
    }

    public function deleteData($id)
    {
        return curl_request(getKkaudioApiUrl() . '/inner/group_chat/remove', ['group_chat_id' => $id], 'urlencoded', 'GET');
    }


    public function getAllFlockRoom($room_id) {
        $where['room.room_id'] = $room_id;
        $res = DB::connection($this->connection)->table($this->table)
            ->leftjoin('group_chat_room', 'group_chat_room.group_chat_id', $this->table.'.group_chat_id')
            ->leftjoin('room', 'group_chat_room.room_id', 'room.room_id')
            ->where($where)
            ->select('group_chat_uid','group_chat_name')
            ->first();
        return $this->objToArray($res);
    }


    public function createGroupRoom($group_id, $room_id)
    {
        $data['room_id'] = $room_id;
        $data['group_chat_id'] = $group_id;
        return curl_request(getKkaudioApiUrl() . '/inner/group_chat/room_bind_group_chat', $data, 'urlencoded', 'GET');
    }

    public function familyMember()
    {
        return $this->hasMany(FamilyMemberModel::class, 'family_id', 'family_id');
    }

    public function number()
    {
        return $this->belongsTo(Number::class, 'user_id', 'user_id');
    }
}
